from loguru import logger


def convert_file_encoding(src_file, dst_file, src_encoding="gbk", dst_encoding="utf-8"):
    """
    转换文件编码

    参数:
        src_file: 源文件路径
        dst_file: 目标文件路径
        src_encoding: 源文件编码,默认utf-8
        dst_encoding: 目标文件编码,默认gbk
    """
    try:
        # 读取源文件
        with open(src_file, "r", encoding=src_encoding, errors="replace") as f:
            content = f.read()

        # 写入目标文件
        with open(dst_file, "w", encoding=dst_encoding, errors="replace") as f:
            f.write(content)

        logger.success(
            f"转换成功: {src_file} ({src_encoding}) -> {dst_file} ({dst_encoding})"
        )

    except UnicodeError as e:
        logger.error(f"编码转换错误: {str(e)}")
    except IOError as e:
        logger.error(f"文件读写错误: {str(e)}")
